#include <plugin/util/AbstractSecurityContextHandler.h>

#include <QFile>
#include <QByteArray>

namespace antu
{
namespace plugin
{
AbstractSecurityContextHandler::AbstractSecurityContextHandler()
{
}

AbstractSecurityContextHandler::~AbstractSecurityContextHandler()
{

}

void AbstractSecurityContextHandler::initFromFile(const QString &fileName)
{
    QFile file(fileName);

    if (file.exists())
    {
        if (file.open(QIODevice::ReadOnly))
        {
            QByteArray content = file.readAll();
            const char* p = content.data();
            this->initFromContent(p, content.length());
        }
    }
}

bool AbstractSecurityContextHandler::isAllowed(const QString &moduleCode) const
{
    auto iter = this->m_oSecurityContexts.find(moduleCode);
    if (iter == this->m_oSecurityContexts.end())
        return false;
    else
        return iter->second->isAllowed();
}

bool AbstractSecurityContextHandler::isAllowed(const QString &moduleCode, const QString &unit) const
{
    auto iter = this->m_oSecurityContexts.find(moduleCode);
    if (iter == this->m_oSecurityContexts.end())
        return false;
    else
        return iter->second->isAllowed(unit);
}

NullSecurityContextHandler::NullSecurityContextHandler()
{

}

NullSecurityContextHandler::~NullSecurityContextHandler()
{

}

void NullSecurityContextHandler::initFromContent(const char* /*p*/, size_t /*length*/)
{

}

bool NullSecurityContextHandler::isAllowed(const QString &/*moduleCode*/) const
{
    return true;
}

bool NullSecurityContextHandler::isAllowed(const QString &/*moduleCode*/, const QString &/*unit*/) const
{
    return true;
}

}
}
